//
//  YWDetailHomeViewController.m
//  百布业务
//
//  Created by 陈炳桦 on 15/10/30.
//  Copyright © 2015年 陈炳桦. All rights reserved.
//  我的发布 需求详情

#import "YWDetailHomeViewController.h"
#import "YWMyDemand.h"
#import "YWDetail1TableViewController.h"
#import "YWDetail2TableViewController.h"
#import "YWDetail3TableViewController.h"

@interface YWDetailHomeViewController ()
/** 标签栏底部的紫色指示器 */
@property (nonatomic, weak) UIView *indicatorView;
/** 当前选中的按钮 */
@property (nonatomic, weak) UIButton *selectedButton;
/** 顶部的所有标签 */
@property (nonatomic, weak) UIView *titlesView;
/** 底部的所有内容 */
@property (nonatomic, weak) UIView *contentView;
@end

@implementation YWDetailHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUp];
    
    // 设置顶部的标签栏
    [self setupTitlesView];
    
    // 初始化子控制器
    [self setupChildVC];
    
    // 底部的contentView
    [self setupContentView];
}

- (void)setUp{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
     // 设置标题
     NSString *text = nil;
     if ([_mydemand.status isEqualToString:@"6"]) { // 已找到
         text = @"已找到";
     }else if ([_mydemand.status isEqualToString:@"0"]) { // 待审核
         text = @"待审核";
     }else if ([_mydemand.status isEqualToString:@"2"]) { // 不通过
         text = @"不通过";
     }else{  // 寻找中
         text = @"寻找中";
     }
    
    if (self.flag){
        self.navigationItem.title = [NSString stringWithFormat:@"需求详情(%@)",text];
    }else{
        self.navigationItem.title = @"需求详情";
    }
    
    
}

//  设置顶部的标签栏
- (void)setupTitlesView
{
    // 标签栏整体
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [UIColor whiteColor] ;
    titlesView.width = self.view.width;
    titlesView.height = 36;
    [self.view addSubview:titlesView];
    
    // 底部的指示器
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = BBRGBColor(85, 26, 175);
    indicatorView.height = 2;
    indicatorView.width = BBScreenW / 3;
    indicatorView.tag = -1;
    indicatorView.y = titlesView.height - indicatorView.height;
    [titlesView addSubview:indicatorView];
    self.indicatorView = indicatorView;
    
    // 内部的子标签
    CGFloat width = BBScreenW / 3;
    CGFloat height = titlesView.height;
    NSArray *array = @[@"需求详情",@"处理状态",@"回复列表"];
    for (NSInteger i = 0; i<3; i++) {
        UIButton *button = [[UIButton alloc] init];
        button.tag = i;
        button.height = height;
        button.width = width;
        button.x = i * width;
        
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:BBRGBColor(85, 26, 175) forState:UIControlStateDisabled];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titlesView addSubview:button];
        
        // 默认点击了第一个按钮
        if (i == 0) {
            button.enabled = NO;
            self.selectedButton = button;
        }
    }
    
}

- (void)titleClick:(UIButton *)button{
    // 修改按钮状态
    self.selectedButton.enabled = YES;
    button.enabled = NO;
    self.selectedButton = button;
    
    // 动画
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.x = button.x;
    }];
    
    // 切换
    [self.contentView addSubview:self.childViewControllers[button.tag].view];
    
}

// 初始化子控制器
- (void)setupChildVC{
    YWDetail1TableViewController *VC1 = [[YWDetail1TableViewController alloc] init];
    [self addChildViewController:VC1];
    
    YWDetail2TableViewController *VC2 = [[YWDetail2TableViewController alloc] init];
    [self addChildViewController:VC2];
    
    
    YWDetail3TableViewController *VC3 = [[YWDetail3TableViewController alloc] init];
    [self addChildViewController:VC3];
    
}

- (void)setupContentView
{
    // 不要自动调整inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIView *contentView = [[UIView alloc] init];
    contentView.frame = CGRectMake(0, 15, BBScreenW, BBScreenH);
    [self.view insertSubview:contentView atIndex:0];
    self.contentView = contentView;
    
    // 添加第一个控制器的view
    [self.contentView addSubview:self.childViewControllers[0].view];
    
}




@end
